<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/12
 * Time: 16:10
 */
namespace App\Models;

use Illuminate\Support\Facades\DB;

Class Coindetail extends BaseModel
{

    protected $table = 'coindetail';
    public $timestamps = false;


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 获取和
     */
    public function getCount($where)
    {
        return $this->multiWhere($where)->count();
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 获得本场礼物所得数量
     */
    public function getBycoindetail($gift,$showId){
         if (!$gift) return;
         foreach ($gift as $k => $v) {
              $where['giftid'] = $v['id'];
              $where['showId'] = $showId;
              $gift[$k]['giftconut'] = $this->getCount($where);
         }
         return $gift;
     }

     /**
      * 周榜
      */
     public function weeklychart($where){
           /**
            *  先分组筛选本周的礼物
            */
           $nowTime = time();
           $afterSevel = $nowTime-24*3600*7;
           $select_sql = "SELECT * FROM ss_coindetail WHERE touid=".$where['uid']." AND showroom=".$where['room']." AND addtime>".$afterSevel."";
           $data = DB::select($select_sql);
           $_tempdata = [];
           $newdatakey = [];
           // 统计分组
           foreach ($data as $v){
                if (in_array($v['uid'],$newdatakey)){
                    $_tempdata[$v['uid']][] = $v;
                }else{
                    // 判断该数组是否有值
                    if(empty($_tempdata[$v['uid']])) {
                        $_tempdata[$v['uid']][] = $v;
                         $newdatakey[] = $v['uid'];
                    }
                }
           }

           $newdata = [];
           // 计算每组的礼物值
           foreach ($_tempdata as $k => $v) {
                 $temp_coin = 0;
                 foreach ($v as $val){
                      $temp_coin+=$val['coin'];
                 }
                 $newdata[$k]['data'] = $v;
                 $newdata[$k]['coin'] = $temp_coin;
           }
           if (empty($newdata)){ return [];}
           $newdata = arraySequence($newdata,'coin','SORT_DESC');
           $member = new Member();
           // 关联用户
           foreach ($newdata as $k=>$v){
                $memberinfo = $member->getOne(['id'=>$v['data'][0]['uid']]);
                $newdata[$k]['member'] = $memberinfo;
           }
           return $newdata;
     }
}